/**
 * Created by MadFrog on 2017/7/5.
 */

Ext.namespace("admin.mental");

admin.mental.exam = function(panelId, title, iconClz){
    // 用户操作标志 默认开启 在clazzStore加载的时候关闭
    var userOprationFlag = true;
    var examStore = new Ext.data.JsonStore({
        url : "admin/exam/find",
        fields : ["id", "title", "mentalName", "closeDate", "description", "createDate"],
        totalProperty : "total",
        root : "data",
        autoLoad : true
    });
    var examSm = new Ext.grid.RowSelectionModel({
        singleSelect : true,
        listeners : {
            rowselect : function () {
                clazzStore.load();
            }
        }
    });
    var examCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header : "测验名称",
        dataIndex : "title",
        width : 150
    }, {
        header : "说明",
        dataIndex : "description",
        width : 200
    }, {
        header : "测验类型",
        dataIndex : "mentalName",
        width : 150
    }, {
        header : "截止时间",
        dataIndex : "closeDate",
        renderer : renderDate
    }, {
        header : "创建时间",
        dataIndex : "createDate",
        renderer : renderDate
    }]);
    var examGrid = new Ext.grid.GridPanel({
        title : "测验列表",
        store : examStore,
        cm : examCm,
        sm : examSm,
        frame : true,
        flex : 5,
        tbar : genOptBarByFn(createExam),
        bbar : genPageBar(examStore)
    });
    var clazzStore = new Ext.data.JsonStore({
        url : "admin/exam/listClazzDto",
        fields : ["id", "name", "studentNumber", "selected"],
        root : "data",
        listeners : {
            beforeload : function(s){
                s.removeAll();
                Ext.apply(s.baseParams, {
                    examId : examSm.getSelected().get("id")
                });
            },
            load : function (store, records) {
                userOprationFlag = false;
                var range = [];
                for(var i=0; i<records.length; i++){
                    var r = records[i];
                    if(r.get("selected")==true){
                        range.push(i);
                    }
                }
                clazzSm.selectRows(range);
                userOprationFlag = true;
            }
        }
    });
    var clazzSm = new Ext.grid.CheckboxSelectionModel({
        checkOnly : true,
        listeners : {
            rowdeselect : function (sm, rowIndex, record) {
                if (userOprationFlag==true){
                    var examId = examSm.getSelected().get("id");
                    var clazzId = record.get("id");
                    ajaxRequest({
                        url : "admin/exam/delClazz/" + examId + "/" + clazzId,
                        callback: function () {
                            clazzStore.reload();
                        }
                    });
                }
            },
            rowselect : function (sm, rowIndex, record) {
                if(userOprationFlag==true){
                    var examId = examSm.getSelected().get("id");
                    var clazzId = record.get("id");
                    ajaxRequest({
                        url : "admin/exam/addClazz/" + examId + "/" + clazzId,
                        callback: function () {
                            clazzStore.reload();
                        }
                    });
                }
            }
        }
    });
    var clazzCm = new Ext.grid.ColumnModel([clazzSm, new Ext.grid.RowNumberer(), {
        header : "班级名称",
        dataIndex : "name",
        width : 200
    }, {
        header : "学生人数",
        dataIndex : "studentNumber",
        width : 60
    }]);
    var clazzGrid = new Ext.grid.GridPanel({
        title : "班级列表",
        store : clazzStore,
        cm : clazzCm,
        sm : clazzSm,
        frame : true,
        flex : 2
    });
    return new Ext.Panel({
        id : panelId,
        title : title,
        iconCls : iconClz,
        closable : true,
        layout : "hbox",
        layoutConfig : {
            align : "stretch"
        },
        items : [examGrid, clazzGrid]
    });

    function createExam() {
        genAddorUpdateWin("admin/exam/add").show();
    }
    function genAddorUpdateWin(url){
        return new Ext.Window({
            title : "新增或修改心理测验",
            layout : "fit",
            modal : true,
            iconCls : "save",
            height : 230,
            width : 290,
            bodyStyle : 'padding:5px;',
            items : genForm(url)
        });
        function genForm(url){
            return new Ext.form.FormPanel({
                url : url,
                labelWidth : 80,
                labelAlign : "right",
                baseCls: 'x-plain',
                defaults : {
                    width : 150
                },
                items : [{
                        xtype : "hidden",
                        name : "id",
                        //value设置-1，为空则controller检验失败
                        value : "-1"
                    }, {
                        xtype : "textfield",
                        fieldLabel : "测验名称",
                        allowBlank : false,
                        name : "title"
                    }, ExtComponentUtil.generateMentalCombo(), {
                        fieldLabel : "截止日期",
                        allowBlank : false,
                        name : "closeDate",
                        xtype : "datefield",
                        format : "Y-m-d"
                    }, {
                        xtype : "textarea",
                        fieldLabel : "说明",
                        allowBlank : false,
                        name : "description"
                }],
                buttonAlign : "center",
                buttons : [{
                    text : "保存",
                    iconCls : "save",
                    handler : function(b){
                        submitForm(b.findParentByType("form"), refreshEventGrid);
                        function refreshEventGrid(){
                            examStore.reload();
                        }
                    }
                }, {
                    text : "重置",
                    iconCls : "reset",
                    type : "reset",
                    handler : function(b){
                        b.findParentByType("form").getForm().reset();
                    }
                }]
            });
        }
    }
}

registerModule("admin.mental.exam", admin.mental.exam);